﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using ATW.MT.CommonClass;
using System.Data;

namespace ATW.MT.Main
{
    public partial class Login : System.Web.UI.Page
    {
        bus_D_user user = new bus_D_user();
        UserInfo u = new UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }
        private void checkLogin()
        {

            string userid = txtUserName.Text.Trim();
            string pwd = txtPsw.Text.Trim();
            try
            {
                if (userid == "")
                {
                    lblMsg.Text = "账号不能为空";
                    return;
                }
                if (pwd == "")
                {
                    lblMsg.Text = "密码不能为空";
                    return;
                }
                if (!common.filterChar(userid) || !common.filterChar(pwd))
                {
                    lblMsg.Text = "账号或者密码中含有非法字符!";
                    return;
                }
                DataTable dt = user.UserLogin(userid);
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["pwd"].ToString() == pwd)
                    {
                        u.UserId = dt.Rows[0]["userid"].ToString();
                        u.UserAccount = dt.Rows[0]["account"].ToString();
                        u.LevelClass = dt.Rows[0]["level_class"].ToString();
                        Session["UserInfo"] = u;
                    }
                    else
                    {
                        lblMsg.Text = "账号或密码错误，请重新输入！";
                        return;
                    }
                }
                else
                {
                    lblMsg.Text = "该账号不存在！";
                    return;
                }
                //Response.Redirect("../BaseManage/UserManager.aspx");
                DataTable dtMenu = user.getModuleList(u.UserId);
                if (dtMenu != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dtMenu.Rows.Count; i++)
                    {
                        if (dtMenu.Rows[i]["module_level"].ToString() == "2")
                        {
                            Response.Redirect(dtMenu.Rows[i]["module_url"].ToString());
                        }
                    }
                }   
            }
            catch (Exception ex)
            {

            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            checkLogin();
        }
    }
}